﻿using MasterMasterMode.Content.Invasions;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.IO;
using Terraria;
using Terraria.GameContent;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;

namespace MasterMasterMode.Common.Systems
{
    public class InvasionSystem : ModSystem
    {
        public static bool hasCheckedForSpawnToday = false;
        public static bool ArmyofwarriorgoldfishesUp = false;
        public static bool DownedArmyofwarriorgoldfishes = false;
        public override void OnWorldLoad()
        {
            hasCheckedForSpawnToday = false;
        }

        public override void OnWorldUnload()
        {
            hasCheckedForSpawnToday = false;
        }
        public override void SaveWorldData(TagCompound tag)
        {
            List<string> downed = [];
            if (DownedArmyofwarriorgoldfishes)
            {
                downed.Add("Armyofwarriorgoldfishes");
            }
            tag.Add("downed", downed);
        }
        public override void LoadWorldData(TagCompound tag)
        {
            IList<string> list = tag.GetList<string>("downed");
            DownedArmyofwarriorgoldfishes = list.Contains("Armyofwarriorgoldfishes");
            hasCheckedForSpawnToday = false;
        }
        public override void NetSend(BinaryWriter writer)
        {
            BitsByte flags = default;
            flags[0] = DownedArmyofwarriorgoldfishes;
            flags[1] = hasCheckedForSpawnToday;
            writer.Write(flags);
        }

        public override void NetReceive(BinaryReader reader)
        {
            BitsByte flags = reader.ReadByte();
            DownedArmyofwarriorgoldfishes = flags[0];
            hasCheckedForSpawnToday = flags[1];
        }
        public override void PostUpdateWorld()
        {
            CheckForNaturalSpawn();
            if (ArmyofwarriorgoldfishesUp)
            {
                if (Main.invasionX == Main.spawnTileX)
                {
                    ArmyofwarriorgoldfishesInvasion.CheckCustomInvasionProgress();
                }
                ArmyofwarriorgoldfishesInvasion.UpdateCustomInvasion();
            }
        }
        private static void CheckForNaturalSpawn()
        {
            if (!DownedArmyofwarriorgoldfishes && !ArmyofwarriorgoldfishesUp && !hasCheckedForSpawnToday && NPC.downedBoss1)
            {
                double currentTime = Main.time;
                bool isInTimeWindow = currentTime >= 27000 && currentTime <= 54000;
                if (isInTimeWindow && Main.dayTime)
                {
                    float spawnChance = 0.05f;

                    if (Main.rand.NextFloat() < spawnChance)
                    {
                        StartArmyofwarriorgoldfishesInvasion();
                        hasCheckedForSpawnToday = true;
                        if (Main.netMode == NetmodeID.Server)
                        {
                            NetMessage.SendData(MessageID.WorldData);
                        }
                    }
                    else
                    {
                        hasCheckedForSpawnToday = true;
                    }
                }
                if (Main.dayTime && currentTime >= 16200 && currentTime < 16230 && hasCheckedForSpawnToday)
                {
                    hasCheckedForSpawnToday = false;
                    if (Main.netMode == NetmodeID.Server)
                    {
                        NetMessage.SendData(MessageID.WorldData);
                    }
                }
            }
        }
        private static void StartArmyofwarriorgoldfishesInvasion()
        {
            ArmyofwarriorgoldfishesUp = true;
            if (Main.netMode == NetmodeID.SinglePlayer)
            {
                Main.NewText(Language.GetTextValue("Mods.MasterMasterMode.Invasion.3"), 175, 75, byte.MaxValue);
            }
            else if (Main.netMode == NetmodeID.Server)
            {
                Terraria.Chat.ChatHelper.BroadcastChatMessage(NetworkText.FromLiteral(Language.GetTextValue("Mods.MasterMasterMode.Invasion.3")), new(175, 75, byte.MaxValue));
            }
        }

        public override void PostDrawInterface(SpriteBatch spriteBatch)
        {
            Player player = Main.LocalPlayer;
            Texture2D texture2D4 = TextureAssets.Extra[9].Value;
            string text7 = "";
            Color c = Color.White;
            bool isInvasion = false;
            if (ArmyofwarriorgoldfishesUp && player.ZoneOverworldHeight)
            {
                texture2D4 = ModContent.Request<Texture2D>("MasterMasterMode/Assets/Textures/Invasion/Armyofwarriorgoldfishes").Value;
                text7 = Language.GetTextValue("Mods.MasterMasterMode.Invasion.4");
                c = new(0, 0, 0);
                isInvasion = true;
            }
            if (Main.invasionProgress == -1)
            {
                return;
            }
            if (Main.invasionProgressMode == 2 && Main.invasionProgressNearInvasion && Main.invasionProgressDisplayLeft < 160)
            {
                Main.invasionProgressDisplayLeft = 160;
            }
            if (Main.invasionProgressAlpha <= 0f)
            {
                return;
            }
            float num18 = 0.5f + Main.invasionProgressAlpha * 0.5f;
            if (isInvasion)
            {
                Vector2 value = FontAssets.MouseText.Value.MeasureString(text7);
                float num19 = 120f;
                if (value.X > 200f)
                {
                    num19 += value.X - 200f;
                }
                Rectangle r3 = Utils.CenteredRectangle(new Vector2(Main.screenWidth - num19, Main.screenHeight - 80), (value + new Vector2(texture2D4.Width + 12, 6f)) * num18);
                Utils.DrawInvBG(spriteBatch, r3, c);
                spriteBatch.Draw(texture2D4, Utils.Left(r3) + Vector2.UnitX * num18 * 8f, null, Color.White * Main.invasionProgressAlpha, 0f, new Vector2(0f, texture2D4.Height / 2), num18 * 0.8f, 0, 0f);
                Utils.DrawBorderString(spriteBatch, text7, Utils.Right(r3) + Vector2.UnitX * num18 * -22f, Color.White * Main.invasionProgressAlpha, num18 * 0.9f, 1f, 0.4f, -1);
            }
        }
    }
}
